﻿Public Class frmElevesProv
    Private szSQL As String
    Private rs As ADODB.Recordset
    Private m_SortingColumn As ColumnHeader
    Private m_SortingIndex As Integer = 0

    Private Sub frmElevesProv_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' largeur form réduite
        Me.Width = 466
        InitListeEleves(0)
        InitComboPermis(CB_CATEGORIES)
        InitComboSocietes(CB_SOCIETES)

        ' utilisateur autorisé à modifier ?
        If gUserAcces < 9 Then
            BO_ENREGISTRER.Enabled = False
        Else
            BO_ENREGISTRER.Enabled = True
        End If

    End Sub

    Private Sub InitListeEleves(ByVal ix As Integer)

        Dim nb As Integer

        ' effacer la listview
        LV_ELEVES.Items.Clear()
        LV_ELEVES.ListViewItemSorter = Nothing

        ' Lire la table FORMULES
        szSQL = "SELECT cd_cli, nompren, permis_demande, societe from ELEVES WHERE agence = '0' ORDER BY permis_demande, nom"
        GetRecordset(ctDSN_AEM, szSQL, rs)

        Do While Not rs.EOF
            LV_ELEVES.Items.Add(rs.Fields("permis_demande").Value)
            nb = LV_ELEVES.Items.Count - 1
            LV_ELEVES.Items(nb).SubItems.Add(Format(rs.Fields("cd_cli").Value, "000000"))
            LV_ELEVES.Items(nb).SubItems.Add(rs.Fields("nompren").Value)
            LV_ELEVES.Items(nb).SubItems.Add(rs.Fields("societe").Value)
            rs.MoveNext()
        Loop
        rs.Close()

        If nb > 0 Then
            LB_NbLignes.Text = CStr(nb) & " lignes"
        End If

        ' restaurer l'ancienne ordre de tri de la listview
        If m_SortingIndex > 0 Then
            SortListView(LV_ELEVES, m_SortingColumn, m_SortingIndex)
        End If
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_ELEVES)

    End Sub

    Private Sub LV_ELEVES_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles LV_ELEVES.ColumnClick
        SortListView(LV_ELEVES, m_SortingColumn, e.Column)
        m_SortingIndex = e.Column
    End Sub

    Private Sub LV_Eleves_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LV_ELEVES.DoubleClick

        If LV_ELEVES.SelectedItems.Count > 0 Then
            ' Lire la fiche Elève
            szSQL = "CALL spGet_ELEVES_byCD_CLI(" & CInt(LV_ELEVES.SelectedItems(0).SubItems(1).Text) & ")"
            GetRecordset(ctDSN_AEM, szSQL, rs)
            If rs.RecordCount > 0 Then
                TE_CD_CLI.Text = rs.Fields("cd_cli").Value
                gCD_CLI = TE_CD_CLI.Text
                TE_NOM.Text = rs.Fields("nom").Value
                TE_PRENOM.Text = rs.Fields("prenom").Value
                CB_CATEGORIES.SelectedIndex = CB_CATEGORIES.FindString(rs.Fields("permis_demande").Value)
                CB_SOCIETES.SelectedIndex = CB_SOCIETES.FindString(rs.Fields("societe").Value)
                If IsDate(rs.Fields("tr_p_ok").Value) Then
                    chkCirculation.Checked = True
                Else
                    chkCirculation.Checked = False
                End If
                TE_MODIFIE_LE.Text = setModifiedBy(rs.Fields("cree_le").Value, rs.Fields("modif_le").Value, rs.Fields("cd_uti").Value)
            End If
            rs.Close()
            ' afficher les détails de la formule
            TC_Details.Visible = True
            ' largeur form élargie
            Me.Width = 912
        End If

    End Sub

    Private Sub LV_Eleves_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles LV_ELEVES.KeyDown
        Select Case e.KeyCode
            ' touche Enter = DoubleClick
            Case Keys.Enter
                LV_Eleves_DoubleClick(sender, e)

            Case Keys.Insert
                BO_ADD_LIGNE_Click(sender, e)

            Case Keys.Delete
                BO_DELETE_LIGNE_Click(sender, e)

        End Select

    End Sub

    Private Sub BO_Enregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ENREGISTRER.Click

        If IsSaisie_OK() = True Then
            ' met à jour
            szSQL = "CALL spUpd_ELEVES_PROV(" & gCD_CLI & "," & TE_CD_CLI.Text & ",'" & _
                nq(TE_NOM.Text) & "','" & nq(TE_PRENOM.Text) & "','" & CB_CATEGORIES.SelectedItem.ToString & "','" & _
                Mid(CB_SOCIETES.SelectedItem.ToString, 1, 1) & "'," & CInt(chkCirculation.Checked) & ",'" & gUserCode & "')"
            ExecQuery(ctDSN_AEM, szSQL)
            gCD_CLI = TE_CD_CLI.Text

            ' Cacher les détails
            BO_ANNULER_Click(sender, e)
        End If

    End Sub

    Private Sub BO_ANNULER_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ANNULER.Click
        ' Cacher les détails
        TC_Details.Visible = False
        ' largeur form réduite
        Me.Width = 466
        ' réaffiche la viewlist
        InitListeEleves(LV_ELEVES.SelectedItems(0).Index)
    End Sub

    Private Sub BO_DELETE_LIGNE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_DELETE_LIGNE.Click
        ' utilisateur autorisé à supprimer ?
        If IsUserAuthorised(9) = False Then
            Exit Sub
        End If
        deleteEnregistrement("ELEVES", TE_NOM.Text, TE_CD_CLI.Text)
        ' Cacher les détails
        BO_ANNULER_Click(sender, e)
    End Sub

    Private Sub BO_ADD_LIGNE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ADD_LIGNE.Click
        ' utilisateur autorisé à ajouter ?
        If IsUserAuthorised(9) = False Then
            Exit Sub
        End If
        ' Ajouter un élève provisoire
        gCD_CLI = 0
        ClearTextBoxes(tb_details)
        CB_CATEGORIES.SelectedIndex = 0
        CB_SOCIETES.SelectedIndex = 0
        chkCirculation.Checked = False
        ' afficher les détails de la formule
        TC_Details.Visible = True
        ' largeur form élargie
        Me.Width = 912
    End Sub

    ' Contrôles de saisie de la fiche ELEVES
    Private Function IsSaisie_OK() As Boolean

        IsSaisie_OK = False

         ' Controle des zones obligatoires
        If TE_CD_CLI.TextLength = 0 Then
            MsgBox("Le code est obligatoire", MsgBoxStyle.Critical)
            TE_CD_CLI.Focus()
            Exit Function
        End If
        If TE_NOM.TextLength = 0 Or TE_PRENOM.TextLength = 0 Or TE_CD_CLI.TextLength = 0 Then
            MsgBox("Le nom et le prénom sont obligatoires", MsgBoxStyle.Critical)
            TE_NOM.Focus()
            Exit Function
        End If
        IsSaisie_OK = True
    End Function

End Class